package com.jasic.view;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.jasic.common.SizeType;
import com.jasic.util.GlobalVariable;
import com.jasic.util.SizeComparator;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;

import static java.util.Collections.sort;

/**
 * Created with IntelliJ IDEA.
 * User: Jasic
 * Date: 12-11-26
 *
 * 创建相机预览
 */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    private SizeType sizeType = SizeType.MEDIUM;

    private String TAG = GlobalVariable.TAG;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or

        /**
         * 设置相机的大小
         */
        Camera.Size size = this.getPreviewSize(this.sizeType);

        this.setPreviewSize(size);
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e) {
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

    /**
     * 设置预览大小
     *
     * @param size
     */
    public void setPreviewSize(Camera.Size size) {
        this.mCamera.getParameters().setPreviewSize(size.width, size.height);
    }

    /**
     * 获取预览大小
     */
    public Camera.Size getPreviewSize(SizeType sizeType) {

        List<Camera.Size> sizes = mCamera.getParameters().getSupportedPreviewSizes();

        StringBuilder sb = new StringBuilder();

        Collections.sort(sizes, new SizeComparator());


sb.append("Camera所有的size:{ ");
for(Camera.Size size :sizes){

    sb.append("[" + size.width + "X" + size.height + "] ");
}
sb.append("}");

System.out.println(sb);


        Camera.Size size = null;
        switch (sizeType) {

            case TINY: {

                size = sizes.get(0);
                break;
            }

            case MEDIUM: {

                size = sizes.get(sizes.size() / 2);
                break;
            }

            case LARGE: {

                size = sizes.get(sizes.size());
                break;
            }
        }
System.out.println("预览分辨率大小：[" + size.width + "X" + size.height +"]");
        return size;
    }
}
